﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Input;

namespace caufingHealth.Forms
{
    public partial class VisitForm : Form
    {
        public PatientInformation.PatientRecord.PatientVisitRecord VisitRecord
        {
            get;
            private set;
        }

        private bool setup = false;

        private bool enterPressed = false;

        public enum MODES {ADD,UPDATE};
        public VisitForm(DoctorInformation[] doctors)
        {
            InitializeComponent();
            VisitRecord = new PatientInformation.PatientRecord.PatientVisitRecord();
            cb_Doctor.DataSource = doctors;
            cb_Doctor.SelectedIndex = 0;
        }

        public VisitForm(MODES mode, PatientInformation.PatientRecord.PatientVisitRecord record, DoctorInformation[] doctors)
            : this(doctors)
        {

            if (mode == MODES.UPDATE)
            {
                setup = true;
                btn_AddVisit.Text = "Update Visit";
                txt_BloodPressure1.Text = record.BloodPressure1;
                txt_BloodPressure2.Text = record.BloodPressure2;
                txt_Pulse.Text = record.Pulse;
                txt_Temperature.Text = record.Temperature;
                txt_Diagnosis.Text = record.Diagnosis;
                cb_Symptoms.Items.AddRange(record.Symptoms.Split(new char[] { ',' }));
                dtp_VisitDateTime.Value = new DateTime(record.VisitDateLong);
                VisitRecord.VisitDateLong = record.VisitDateLong;
                cb_Symptoms.Text = (string)cb_Symptoms.Items[0];
                cb_Doctor.SelectedValue = record.DoctorID;
                setup = false;
            }
        }


        private void lbl_patient_id_Click(object sender, EventArgs e)
        {

        }

        private void cb_Symptoms_KeyPress(object sender, KeyPressEventArgs e)
        {
            
        }

        private void cb_Symptoms_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Boolean exists = false;
                foreach (String symptom in cb_Symptoms.Items)
                    if (symptom.Equals(cb_Symptoms.Text))
                        exists = true;
                if(!exists)
                    if(!String.IsNullOrWhiteSpace(cb_Symptoms.Text))
                        cb_Symptoms.Items.Add(cb_Symptoms.Text);
                    
                cb_Symptoms.Text = "";
                e.Handled = true;
                e.SuppressKeyPress = true;
            }
        }

        private void btn_AddVisit_Click(object sender, EventArgs e)
        {
            VisitRecord.Pulse = txt_Pulse.Text;

            if(!String.IsNullOrWhiteSpace(cb_Symptoms.Text))
                if(MessageBox.Show("You have an unsaved Symptom.  Would you like to save now?", "Unsaved Symptom Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    cb_Symptoms.Items.Add(cb_Symptoms.Text);
            

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < cb_Symptoms.Items.Count; i++)
            {
                if(i == cb_Symptoms.Items.Count-1)
                    builder.Append(cb_Symptoms.Items[i]);
                else
                    builder.Append(cb_Symptoms.Items[i] + ",");
            }
            VisitRecord.Symptoms = builder.ToString();
            VisitRecord.Temperature = txt_Temperature.Text;
            VisitRecord.VisitDate = dtp_VisitDateTime.Value;
            VisitRecord.BloodPressure1 = txt_BloodPressure1.Text;
            VisitRecord.BloodPressure2 = txt_BloodPressure2.Text;
            VisitRecord.Diagnosis = txt_Diagnosis.Text.Replace("'", "\\'"); ;
            VisitRecord.DoctorID = (int)cb_Doctor.SelectedValue;
        }

        private void cb_Symptoms_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(!setup)
                cb_Symptoms.Items.RemoveAt(cb_Symptoms.SelectedIndex);
        }
    }
}
